package com.me.videoservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.me.videoservice.dao.VideoPublishDao;
import com.me.videoservice.model.callback.PublishCallBack;
import com.me.videoservice.model.callback.UnpublishCallBack;
import com.me.videoservice.model.common.CallbackRetCode;
import com.me.videoservice.model.entry.DeviceInfo;
import com.me.videoservice.model.entry.DeviceInfoWithStream;
import com.me.videoservice.model.entry.VideoPublishInfo;
import com.me.videoservice.service.DeviceInfoService;
import com.me.videoservice.service.VideoConnectService;
import com.me.videoservice.service.VideoPublishService;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VideoPublishServiceImpl extends
    ServiceImpl<VideoPublishDao, VideoPublishInfo> implements
    VideoPublishService {

  @Autowired
  private DeviceInfoService deviceInfoService;
  @Autowired
  private VideoConnectService videoConnectService;

  @Override
  public List<DeviceInfoWithStream> queryAll() {
    return deviceInfoService.queryAllInfoWithPublish();
  }

  @Override
  public int publish(PublishCallBack callback) {
    if (videoConnectService.checkClientAndStream(callback.getClientId(), callback.getStream())!=0) {
      return CallbackRetCode.NO_STREAM.getCode();
    }
    LocalDateTime now = LocalDateTime.now();
    if (this.lambdaQuery()
        .eq(VideoPublishInfo::getStream, callback.getStream())
        .eq(VideoPublishInfo::getApp, callback.getApp())
        .eq(VideoPublishInfo::getVhost, callback.getVhost()).count() == 0) {
      this.save(new VideoPublishInfo(
          null,
          callback.getClientId(),
          null,
          callback.getIp(),
          callback.getVhost(),
          callback.getApp(),
          callback.getStream(),
          1, now, null));
    } else {
      this.lambdaUpdate()
          .set(VideoPublishInfo::getPushFlag, 1)
          .set(VideoPublishInfo::getPushSDate, now)
          .set(VideoPublishInfo::getPushEDate, null)
          .eq(VideoPublishInfo::getStream, callback.getStream())
          .eq(VideoPublishInfo::getApp, callback.getApp())
          .eq(VideoPublishInfo::getVhost, callback.getVhost())
          .eq(VideoPublishInfo::getClientId, callback.getClientId())
          .update();
    }
    deviceInfoService.lambdaUpdate()
        .set(DeviceInfo::getPushFlag, 1)
        .set(DeviceInfo::getUpdatedAt, now)
        .eq(DeviceInfo::getStreamCode, callback.getStream())
        .update();
    return 0;
  }

  @Override
  public void unPublish(UnpublishCallBack callback) {
    LocalDateTime now = LocalDateTime.now();
    this.lambdaUpdate()
        .set(VideoPublishInfo::getPushFlag, 2)
        .set(VideoPublishInfo::getPushEDate, now)
        .eq(VideoPublishInfo::getStream, callback.getStream())
        .eq(VideoPublishInfo::getApp, callback.getApp())
        .eq(VideoPublishInfo::getVhost, callback.getVhost())
        .update();
    deviceInfoService.lambdaUpdate()
        .set(DeviceInfo::getPushFlag, 2)
        .set(DeviceInfo::getUpdatedAt, now)
        .eq(DeviceInfo::getStreamCode, callback.getStream())
        .update();
  }
}
